/*
 * AllwinnerTech hdmi cec driver
 *
 * Copyright (C) 2015 AllwinnerTech Corporation
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

#include <linux/device.h>
#include <linux/sched.h>
#include <linux/kthread.h>
#include <linux/err.h>
#include <linux/delay.h>
#include <linux/platform_device.h>
#include <linux/errno.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/types.h>
#include <linux/module.h>
#include <linux/gpio.h>

#ifdef __SUNXI__
#include <mach/sys_config.h>
#include <linux/pinctrl/pinctrl.h>
#include <linux/pinctrl/consumer.h>
#include <linux/pinctrl/pinconf-sunxi.h>
#endif

static int hdmi_cec_portid = -1;
int hdmi_cec_line_init(int portid)
{
	int ret = 0;

	ret = gpio_request(portid, "hdmi-cec-gpio");
	if (ret != 0) {
		pr_err("%s: request hdmi-cec-gpio failed\n", __func__);
		return ret;
	}

	hdmi_cec_portid = portid;
	return 0;
}

#ifdef __SUNXI__
#define TYPE_IN		0
#define TYPE_OUT	1
#define TYPE_EINT	6
static void hdmi_cec_line_config(int portid, int type)
{
	long unsigned int config;
	char name[8];

	sunxi_gpio_to_name(portid, name);
	config = SUNXI_PINCFG_PACK(SUNXI_PINCFG_TYPE_FUNC, 0xFFFF);
	pin_config_get(SUNXI_PINCTRL, name, &config);

	if (type != SUNXI_PINCFG_UNPACK_VALUE(config)){
      config = SUNXI_PINCFG_PACK(SUNXI_PINCFG_TYPE_FUNC, type);
	  pin_config_set(SUNXI_PINCTRL, name, config);;
	}

	if (type == TYPE_IN) {
		config = SUNXI_PINCFG_PACK(SUNXI_PINCFG_TYPE_PUD, 0xFFFF);
		pin_config_get(SUNXI_PINCTRL, name, &config);

      	config = SUNXI_PINCFG_PACK(SUNXI_PINCFG_TYPE_PUD, 0x01);
	  	pin_config_set(SUNXI_PINCTRL, name, config);;
	}
}

void config_cec_line_as_input(void)
{
	hdmi_cec_line_config(hdmi_cec_portid, TYPE_IN);
}

void config_cec_line_as_output(void)
{
	hdmi_cec_line_config(hdmi_cec_portid, TYPE_OUT);
}

int get_cec_line_state(void)
{

//	volatile int *data   = (volatile int *)(0xf1c20800 + 0x10);
//	int reg = *data;

//	reg = reg & (1<<21);
//	return (reg!=0)?1:0;
	return __gpio_get_value(hdmi_cec_portid);
}

void set_cec_line_state(int level)
{
	__gpio_set_value(hdmi_cec_portid, level);
}
#else

void config_cec_line_as_input(void)  { return; }
void config_cec_line_as_output(void) { return; }
void set_cec_line_state(int level)   { return; }

int get_cec_line_state(void)        { return 0; }
int hdmi_hotplug_state(void) 		{ return 0; }

void cec_line_irq_disable(void)	{ return; }
void cec_line_irq_enable(void)	{ return; }

#endif
